// This is the outer power crystal, which will spin around, and cause damage to the
// player if they touch it
datablock ItemData(SpinningPowerCrystal)
{
	category            = "PowerCrystal";
	shapeFile           = "~/data/shapes/gem/gem.dts";
	directDamage        = 5;
	radiusDamage        = 5;
	damageRadius        = 1.0;
	areaImpulse         = 2000;

	hasLight            = true;
	lightRadius         = 6;
	lightColor          = "1 0 0 1";
	lightType           = "ConstantLight";
};

datablock AudioProfile(SpinningPowerCrystalZap)
{
	filename = "~/data/shapes/jail/zap.wav";
	description = AudioClose3d;
	preload = true;
};

// Cause damage to occur when something touches the laser bar
function SpinningPowerCrystal::onCollision(%this,%obj,%col,%fade,%pos,%normal)
{
	if (%col.getType() & $TypeMasks::ShapeBaseObjectType)
	  %col.damage(%obj,%pos,%this.directDamage,"SpinningPowerCrystal");

	serverPlay3D(SpinningPowerCrystalZap, %obj.getTransform());
}







// This is an an invisible power crystal which will actually take damage, and can be destroyed
// Once it's destroyed, we will hide the spinning power crystal
datablock StaticShapeData(DamagablePowerCrystal)
{
	category            = "PowerCrystal";
	shapeFile           = "~/data/shapes/gem/gem.dts";
	directDamage        = 0;
	radiusDamage        = 0;
	damageRadius        = 0;
	areaImpulse         = 0;
	
	maxDamage			= 100;
	damageType			= "PowerCrystalDebris";
};

datablock AudioProfile(SpinningPowerCrystalHit)
{
	filename = "~/data/sound/crystal_hit.wav";
	description = AudioClose3d;
	preload = true;
};

datablock AudioProfile(SpinningPowerCrystalShatter)
{
	filename = "~/data/sound/crystal_shatter.wav";
	description = AudioClose3d;
	preload = true;
};

datablock ParticleData(PowerCrystalDebrisTrail)
{
   textureName          = "~/data/shapes/particles/fire";
   dragCoefficient      = 1;
   gravityCoefficient   = 0;
   inheritedVelFactor   = 0;
   windCoefficient      = 0;
   constantAcceleration = 0;
   lifetimeMS           = 800;
   lifetimeVarianceMS   = 100;
   spinSpeed     = 0;
   spinRandomMin = -90.0;
   spinRandomMax =  90.0;
   useInvAlpha   = true;

   colors[0]     = "0.8 0.0 0.0 1.0";
   colors[1]     = "0.5 0.0 0.0 0.7";
   colors[2]     = "0.1 0.0 0.0 0.5";

   sizes[0]      = 0.3;
   sizes[1]      = 0.2;
   sizes[2]      = 0.1;

   times[0]      = 0.1;
   times[1]      = 0.2;
   times[2]      = 1.0;
};

datablock ParticleEmitterData(PowerCrystalDebrisTrailEmitter)
{
   ejectionPeriodMS = 30;
   periodVarianceMS = 0;
   ejectionVelocity = 0.0;
   velocityVariance = 0.0;
   ejectionOffset   = 0.0;
   thetaMin         = 170;
   thetaMax         = 180;
   phiReferenceVel  = 0;
   phiVariance      = 360;
   lifetimeMS       = 2000;
   particles = "PowerCrystalDebrisTrail";
};

datablock DebrisData(PowerCrystalExplosionDebris)
{
   shapeFile = "~/data/shapes/gem/debris.dts";
   emitters = "PowerCrystalDebrisTrailEmitter";
   
   elasticity = 0.6;
   friction = 0.5;
   numBounces = 1;
   bounceVariance = 1;
   explodeOnMaxBounce = false;
   staticOnMaxBounce = false;
   snapOnMaxBounce = false;
   minSpinSpeed = 10;
   maxSpinSpeed = 700;
   render2D = false;
   lifetime = 4;
   lifetimeVariance = 2;
   velocity = 12;
   velocityVariance = 6;
   fade = true;
   useRadiusMass = true;
   baseRadius = 0.3;
   gravModifier = 0.5;
   terminalVelocity = 6;
   ignoreWater = true;
};

datablock ExplosionData(PowerCrystalExplosion)
{
   soundProfile = SpinningPowerCrystalShatter;
   lifeTimeMS = 800;

   // Camera Shaking
   shakeCamera = true;
   camShakeFreq = "10.0 11.0 10.0";
   camShakeAmp = "1.0 1.0 1.0";
   camShakeDuration = 0.5;
   camShakeRadius = 10.0;

   // Exploding debris
   debris = PowerCrystalExplosionDebris;
   debrisThetaMin = 0;
   debrisThetaMax = 60;
   debrisPhiMin = 0;
   debrisPhiMax = 360;
   debrisNum = 60;
   debrisNumVariance = 5;
   debrisVelocity = 12;
   debrisVelocityVariance = 2.5;
   
   // Impulse
   impulseRadius = 5;
   impulseForce = 25;

   // Dynamic light
   lightStartRadius = 6;
   lightEndRadius = 3;
   lightStartColor = "0.5 0 0";
   lightEndColor = "0.2 0 0";
};

function DamagablePowerCrystal::OnAdd(%this, %powerCrystal)
{
	// ambient sound for the power core room
	new AudioEmitter(PowerCrystalHum) 
	{
		canSaveDynamicFields = "1";
		position = %powerCrystal.getTransform();
		rotation = "1 0 0 0";
		scale = "1 1 1";
		useProfileDescription = "0";
		fileName = "~/data/sound/power_room.wav";
		type = "2";
		volume = "1";
		outsideAmbient = "1";
		referenceDistance = "15";
		maxDistance = "20";
		isLooping = "1";
		loopCount = "-1";
		minLoopGap = "0";
		maxLoopGap = "0";
		enableVisualFeedback = "1";
		is3D = "1";
		coneInsideAngle = "360";
		coneOutsideAngle = "360";
		coneOutsideVolume = "0.1";
		coneVector = "0 1 0";
	};
	%powerCrystal.setCloaked(true);
}

function DamagablePowerCrystal::Damage(%this, %powerCrystal, %sourceObject, %pos, %damage, %damageType)
{
	%powerCrystal.applyDamage(%damage);
	if (%powerCrystal.getDamageLevel() >= %this.maxDamage)
		%this.Explode(%powerCrystal, %pos);
	else
		serverPlay3D(SpinningPowerCrystalHit, %powerCrystal.getTransform());
}

function DamagablePowerCrystal::Explode(%this, %powerCrystal, %pos)
{
	if (%powerCrystal.exploded)
		return;
	
	%powerCrystal.exploded = true;
	%boom = new Explosion()
	{
		datablock		= PowerCrystalExplosion;
		position		= %pos;
		sourceObject	= %powerCrystal.sourceObject;
		sourceSlot		= %powerCrystal.sourceSlot;
		client			= %powerCrystal.client;
	};
	MissionCleanup.add(%boom);
	%powerCrystal.setHidden(true);
	
	// Hide the spinning power crystal
	if (%powerCrystal.spinningCrystal !$= "")
	{
		%spinningCrystal = nameToId(%powerCrystal.spinningCrystal);
		if (%spinningCrystal != -1) 
			%spinningCrystal.setHidden(true);
	}
	
	// Delete the ambient sound; loop the warning beep
	PowerCrystalHum.delete();
	if (!alxIsPlaying(%WarningBeepLoop))
		alxPlay(WarningBeepLoop);
		
	// Unfortunately, this doesn't look good =/
	//goAlarmMode("MissionGroup");
}

function goAlarmMode(%path)
{	
	if ((%role=nameToID(%path)) >= 0)
	{
		%count = %role.getCount();
		for ( %i = 0; %i < %count; %i++)
		{
			// If it's an interior object, try setting it's alarm mode
			if (%role.getObject(%i).getClassName() $= "InteriorInstance")
			{
				%role.getObject(%i).setAlarmMode("On");
			}
			
			// Otherwise, see if this has any children we should set the alarm mode on
			else
			{
				goAlarmMode(%path @ "/" @ %role.getObject(%i).getName());
			}
		}
	}
}